package com.jk.customer.service.impl;

import com.jk.customer.dao.PurchaseDao;
import com.jk.customer.entity.*;
import com.jk.customer.service.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@Service
public class PurchaseServiceImpl implements PurchaseService {
    @Autowired
    private PurchaseDao purchaseDao;

    /**
     *
     * @param product
     * @return
     * @throws ParseException
     */
    @Override
    public List<ProductEntity> getPicture(ProductEntity product,HttpServletRequest request) throws ParseException {


        UserEntity user = (UserEntity) request.getSession().getAttribute("user");

        List<ProductEntity> list = purchaseDao.getPicture(product,user.getUserId());

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        Date date = new Date();
        long time = date.getTime();

        for (int i = 0; i < list.size(); i++) {
            String startDate = list.get(i).getQueryPictureStartDate();
            Date parse = simpleDateFormat.parse(startDate);
            long dateTime = parse.getTime();

            String endDate = list.get(i).getQueryPictureEndDate();
            Date endTime = simpleDateFormat.parse(endDate);
            long endDateTime = endTime.getTime();

            if (list.get(i).getQueryPictureState()!=4 && list.get(i).getQueryPictureState()!=6 && list.get(i).getQueryPictureState()!=7 && list.get(i).getQueryPictureState()!= 8 && list.get(i).getQueryPictureState()!=10){
                if(time >= dateTime && time < endDateTime){
                    purchaseDao.updateState(list.get(i).getQueryPictureId());
                    list.get(i).setQueryPictureState(5);
                }

                if (time > endDateTime){
                    purchaseDao.updateStateEnd(list.get(i).getQueryPictureId());
                    list.get(i).setQueryPictureState(9);
                }
            }
        }
        return list;
    }

    /**
     * 新增方法
     * @param product
     * 蔡运通
     */
    @Override
    public void addPicture_cyt(ProductEntity product,HttpServletRequest request) {

        if(product.getProductId().equals("")){
            product.setProductId(null);
        }


        if (product.getProductId() == null){

            if (product.getInletPressureMinKpaMpa() == 1){
                product.setInletPressureMin(product.getInletPressureMin()*1000);
            }
            if (product.getInletPressureMinKpaMpa() == 2){
                product.setInletPressureMin(product.getInletPressureMin()*10000);
            }

            if (product.getInletPressureMaxKpaMpa() == 1){
                product.setInletPressureMax(product.getInletPressureMax()*1000);
            }
            if (product.getInletPressureMaxKpaMpa() == 2){
                product.setInletPressureMax(product.getInletPressureMax()*10000);
            }

            if (product.getOutletPressureMinKpaMpa() == 1){
                product.setOutletPressureMin(product.getOutletPressureMin()*1000);
            }
            if (product.getOutletPressureMinKpaMpa() == 2){
                product.setOutletPressureMin(product.getOutletPressureMin()*10000);
            }

            if (product.getOutletPressureMaxKpaMpa() == 1){
                product.setOutletPressureMax(product.getOutletPressureMax()*1000);
            }
            if (product.getOutletPressureMaxKpaMpa() == 2){
                product.setOutletPressureMax(product.getOutletPressureMax()*10000);
            }


            /*if(product.getPressureRegulatorType() == 3){
               product.setProductBrand(product.getOtherRegulatorType());
          }*/

            if (product.getProductCategory().equals("其他") && !product.getOtherType().equals("")){
                product.setProductCategory(product.getOtherType());
            }
            UUID uuid = UUID.randomUUID();
            product.setProductId(uuid.toString().replaceAll("-",""));
            request.getSession().setAttribute("uuid",uuid.toString().replaceAll("-",""));
            System.err.println(uuid.toString().replaceAll("-",""));

            UserEntity user = (UserEntity)request.getSession().getAttribute("user");
            product.setCommonUserId(user.getUserId());

            purchaseDao.addPicture_cyt(product);
        }else{
            purchaseDao.updatePicture_cyt(product);
        }

    }

    /**
     * 新增寻图表
     * @param picture
     * 修改人：蔡运通
     */
    @Override
    public void addPicturefindImg_cyt(PictureEntity picture,HttpServletRequest request) {

        if (picture.getQueryPictureId().equals("")){
            picture.setQueryPictureId(null);
        }

        if (picture.getQueryPictureId() == null){
            UUID uuid = UUID.randomUUID();
            picture.setQueryPictureId(uuid.toString().replaceAll("-",""));
            Object uuid1 = request.getSession().getAttribute("uuid");
            picture.setQueryPictureRequirementType(uuid1.toString());
            purchaseDao.addPicturefindImg_cyt(picture);
        }else{
            purchaseDao.updatePicturefindImg_cyt(picture);
        }
    }

    /**
     * 删除寻图列表
     * @param productId
     * 修改人：蔡运通
     */
    @Override
    public void delById_cyt(String productId) {

        purchaseDao.delById_cyt(productId);

        /* 删除 主表数据 */
        purchaseDao.delByProductId_cyt(productId);
    }

    /**
     * 取消寻图数据
     * @param productId
     * 修改人：蔡运通
     */
    @Override
    public void cancelById_cyt(String productId) {

        purchaseDao.cancelById_cyt(productId);
    }

    /**
     * 方法描述：重新发起寻图单
     * @param productId
     * 修改人：蔡运通
     */
    @Override
    public void toInitiatePicture_cyt(String productId) {

        purchaseDao.toInitiatePicture_cyt(productId);
    }

    /**
     * 寻图回显
     * @param productId
     * @return
     * 修改人：蔡运通
     */
    @Override
    public ProductEntity selById_cyt(String productId) {

        return purchaseDao.selById_cyt(productId);
    }

    /**
     * 方法描述：寻图单回显
     * @param productId
     * @return
     * 修改人 蔡运通
     */
    @Override
    public PictureEntity selByPictureId_cyt(String productId) {

        return purchaseDao.selByPictureId_cyt(productId);
    }

    /**
     * 方法描述（材料表新增）
     * @param materialsEntity
     * 修改人：蔡运通
     */
    @Override
    public void addmaterials_cyt(MaterialsEntity materialsEntity,HttpServletRequest request) {

        /* 用户name */
        UserEntity user = (UserEntity)request.getSession().getAttribute("user");
        materialsEntity.setMaterialsUserName(user.getUserRealName());

        /* 总表关联ID */
        Object uuid = request.getSession().getAttribute("uuid");
        materialsEntity.setCommonQueryId(uuid.toString());

        /* 询价表ID */
        Object priceUuid = request.getSession().getAttribute("priceUuid");
        materialsEntity.setQueryPriceId(priceUuid.toString());

        purchaseDao.addmaterials_cyt(materialsEntity);
    }

    /**
     * 方法描述（延期回显）
     * @param productId
     * @return
     * 修改人：蔡运通
     */
    @Override
    public PictureEntity selYanQiById(String productId) {

        return purchaseDao.selYanQiById(productId);
    }

    /**
     * 方法描述（我要比价）
     * @param id
     * @return
     * 修改人：蔡运通
     */
    @Override
    public List<MaterialsEntity> getuserByTypeOne(String id) {

        return purchaseDao.getuserByTypeOne(id);
    }

    /**
     * 方法描述（延期）
     * @param product
     * 修改人：蔡运通
     */
    @Override
    public void updateYanQi(ProductEntity product) {

        purchaseDao.updateYanQi(product);
    }

    /**
     * 方法描述（查看）
     * @param productId
     * @return
     * 修改人：蔡运通
     */
    @Override
    public List<PictureFile> findImgData_cyt(String productId) {
        List<PictureFile> list =  purchaseDao.findImgData_cyt(productId);

        List<PictureFile> listPic = new ArrayList<>();


        for (PictureFile file : list) {
            String user =  purchaseDao.findUserName(file.getUserId()) ;

            PictureFile pictureFile = purchaseDao.findImgUrl(file.getUserId(),productId);
            PictureFile pictureFile1 = new PictureFile();
            pictureFile1.setUserRealName(user);
            pictureFile1.setPictureName(pictureFile.getPictureName());
            pictureFile1.setCommonId(productId);
            listPic.add(pictureFile1);
        }
        return listPic ;
    }

    /**
     * 方法描述（查看材料单）
     * @param commonId
     * @return list
     * 修改人：蔡运通
     */
    @Override
    public List<ProductEntity> findMaterials_cyt(String commonId, String userRealName) {

        List<ProductEntity> list = purchaseDao.findMaterials_cyt(commonId,userRealName);

        return list;
    }

    /**
     * 方法描述（确定定图）
     * @param queryPictureId
     * 修改人：蔡运通
     */
    @Override
    public void confirmFindImg_cyt(String queryPictureId,String userId,String commonId) {

        purchaseDao.confirmFindImg_cyt(queryPictureId);

        Integer id = purchaseDao.findUserId(userId);
        purchaseDao.updateIsSelect(id,commonId);
    }

    /**
     * 方法描述（查看详情）
     * @param productId
     * @return
     * 修改人：蔡运通
     */
    @Override
    public List<ProductEntity> findParticularsData(String productId) {

        return purchaseDao.findParticularsData(productId);
    }

    /**
     * 方法描述（查询我的材料单）
     * @return list
     * 修改人：蔡运通
     */
    @Override
    public List<MaterialsEntity> findBillOfMaterialsData(HttpServletRequest request) {

        UserEntity user = (UserEntity) request.getSession().getAttribute("user");

        return purchaseDao.findBillOfMaterialsData(user.getUserId());
    }

    /**
     * 方法描述(查询总共的页面)
     * @return list
     * 修改人：蔡运通
     */
    @Override
    public List<ProductEntity> findTotalPages(HttpServletRequest request) {

        List<ProductEntity> list = purchaseDao.findTotalPages();

        return list;
    }

    /**
     * 方法描述（所有材料单查询）
     * @return list
     * 修改人：蔡运通
     */
    @Override
    public List<ProductEntity> total_materials() {

        return purchaseDao.total_materials();
    }
}